﻿Public Class FrmBTercero
    Private Shared ter As GBDZafiro.Tercero

    Public ReadOnly Property Tercero As GBDZafiro.Tercero
        Get
            Return ter
        End Get
    End Property

    Private Sub FrmBTercero_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarGvTerceros(GBDZafiro.Tercero.ConsxNitApeNomLike(tbFiltro.Text))
        tbFiltro.Focus()
    End Sub

    Private Sub btCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btCancelar.Click
        ter = Nothing
        Me.Close()
    End Sub

    Private Sub btAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btAceptar.Click
        Dim dtTercero As DataTable = GBDZafiro.Tercero.ConsxNitApeNomLike(tbFiltro.Text)
        cargarGvTerceros(dtTercero)
        gvTerceros.Focus()
    End Sub

    Private Sub gvTerceros_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvTerceros.CellDoubleClick
        sendTercero()
    End Sub

    Private Sub gvTerceros_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles gvTerceros.KeyDown
        If e.KeyCode = Keys.Enter Then
            sendTercero()
        End If
    End Sub

    Private Sub tbFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbFiltro.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Dim dtTercero As DataTable = GBDZafiro.Tercero.ConsxNitApeNomLike(tbFiltro.Text)
            cargarGvTerceros(dtTercero)
        End If
    End Sub

    Private Sub cargarGvTerceros(ByRef dtTercero As DataTable)
        If dtTercero.Rows.Count < 1 Then
            lbNoDatos.Visible = True
            gvTerceros.DataSource = Nothing

        Else
            lbNoDatos.Visible = False
            With gvTerceros
                .DataSource = Nothing
                .Rows.Clear()
                .DataSource = dtTercero
            End With
        End If
    End Sub

    Private Sub sendTercero()
        Dim tercero As New GBDZafiro.Tercero
        Dim rgvTercero As New DataGridViewRow()
        rgvTercero = gvTerceros.CurrentRow
        tercero.ID_TERCERO = rgvTercero.Cells.Item("ID_TERCERO").Value
        tercero.NIT = rgvTercero.Cells.Item("NIT").Value
        tercero.RAZON_SOCIAL = rgvTercero.Cells.Item("RAZON_SOCIAL").Value
        tercero.DIRECCION = rgvTercero.Cells.Item("DIRECCION").Value.ToString
        tercero.TELEFONO1 = rgvTercero.Cells.Item("TELEFONO1").Value.ToString
        tercero.CLASE_REGIMEN = rgvTercero.Cells.Item("CLASE_REGIMEN").Value.ToString
        tercero.COD_LISTA_PRECIO = rgvTercero.Cells.Item("COD_LISTA_PRECIO").Value.ToString
        tercero.ID_MUNICIPIO = rgvTercero.Cells.Item("ID_MUNICIPIO").Value
        tercero.ES_AUTORETENEDOR = rgvTercero.Cells.Item("ES_AUTORETENEDOR").Value.ToString
        tercero.ES_REG_ESPECIAL = rgvTercero.Cells.Item("ES_REG_ESPECIAL").Value.ToString
        tercero.NOM_CLASE_REGIMEN = rgvTercero.Cells.Item("NOM_CLASE_REGIMEN").Value.ToString
        ter = tercero
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
  
End Class